<template>
  <div class="change-password-container">
    <el-card class="password-card">
      <template #header>
        <div class="card-header">
          <h2>修改密码</h2>
        </div>
      </template>

      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="120px"
        class="password-form"
      >
        <el-form-item label="当前密码" prop="oldPassword">
          <el-input
            v-model="formData.oldPassword"
            type="password"
            show-password
            :prefix-icon="'el-icon-lock'"
            placeholder="请输入当前密码"
          />
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="formData.newPassword"
            type="password"
            show-password
            :prefix-icon="'el-icon-lock'"
            placeholder="请输入新密码"
          />
        </el-form-item>

        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
            v-model="formData.confirmPassword"
            type="password"
            show-password
            :prefix-icon="'el-icon-lock'"
            placeholder="请确认新密码"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="handleSubmit"
            :loading="loading"
            class="submit-btn"
          >
            确认修改
          </el-button>
          <el-button @click="$router.push('/personnel')" plain>
            返回
          </el-button>
        </el-form-item>
      </el-form>

      <div class="password-tips">
        <h4>密码要求：</h4>
        <ul>
          <li>长度至少6个字符</li>
          <li>包含数字和字母</li>
          <li>不能与当前密码相同</li>
        </ul>
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive, watch } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'

export default {
  name: 'ChangePassword',
  setup() {
    const router = useRouter()
    const store = useStore()
    const formRef = ref(null)
    const loading = ref(false)

    const formData = reactive({
      oldPassword: '',
      newPassword: '',
      confirmPassword: ''
    })

    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (value.length < 6) {
        callback(new Error('密码长度不能小于6个字符'))
      } else if (!/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/.test(value)) {
        callback(new Error('密码必须包含数字和字母'))
      } else if (value === formData.oldPassword) {
        callback(new Error('新密码不能与当前密码相同'))
      } else {
        callback()
      }
    }

    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== formData.newPassword) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }

    const rules = {
      oldPassword: [
        { required: true, message: '请输入当前密码', trigger: 'blur' }
      ],
      newPassword: [
        { required: true, validator: validatePass, trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, validator: validatePass2, trigger: ['blur', 'change'] }
      ]
    }

    watch(() => formData.newPassword, (newVal) => {
      if (formData.confirmPassword !== '') {
        formRef.value?.validateField('confirmPassword')
      }
    })

    const handleSubmit = async () => {
      if (!formRef.value) return
      
      try {
        await formRef.value.validate()
        loading.value = true
        
        await store.dispatch('changePassword', formData)
        ElMessage.success('密码修改成功，请重新登录')
        store.commit('logout')
        router.push('/login')
      } catch (error) {
        ElMessage.error(error.message || '密码修改失败')
      } finally {
        loading.value = false
      }
    }

    return {
      formRef,
      formData,
      loading,
      rules,
      handleSubmit
    }
  }
}
</script>

<style scoped>
.change-password-container {
  padding: 20px;
  max-width: 600px;
  margin: 0 auto;
}

.password-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-header h2 {
  margin: 0;
  font-size: 20px;
  color: #303133;
}

.password-form {
  padding: 20px 0;
}

:deep(.el-input__inner) {
  height: 40px;
  line-height: 40px;
}

.submit-btn {
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  border: none;
  padding: 12px 25px;
  transition: all 0.3s;
}

.submit-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.password-tips {
  margin-top: 20px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.password-tips h4 {
  margin: 0 0 10px;
  color: #606266;
}

.password-tips ul {
  margin: 0;
  padding-left: 20px;
  color: #909399;
}

.password-tips li {
  line-height: 1.8;
}
</style> 